package com.ha.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ha.dto.BannerDTO;
import com.ha.dto.BannerPageDTO;
import com.ha.entity.Banner;
import com.ha.entity.Employee;
import com.ha.exception.BaseException;
import com.ha.mapper.BannerMapper;
import com.ha.result.PageResult;
import com.ha.service.BannerService;
import com.ha.vo.BannerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: BannerServiceImpl
 * Package: com.ha.service.impl
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/5/2  19:23
 * @Version 1.0
 */
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;
    @Override
    public void addBanner(Banner banner) {
        banner.setDeleted(1);
        bannerMapper.addBanner(banner);
    }

    @Override
    public PageResult pageQuery(BannerPageDTO bannerDTO) {
        //开始分页查询,动态添加limit 和计算 来实现查询,基于拦截器
        PageHelper.startPage(bannerDTO.getPage(),bannerDTO.getPageSize());
        Page<Banner> page = bannerMapper.pageQuery(bannerDTO);
        long total = page.getTotal();
        List<Banner> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void delete(Long id ,Integer deleted) {
        if(id != null){
            if(bannerMapper.getById(id) != null ){
                bannerMapper.deleteById(id,deleted);
            }else {
                throw new BaseException("没有该id的图片");
            }
        }
    }

    @Override
    public void update(BannerDTO bannerDTO) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerDTO,banner);
        banner.setDeleted(1);
        bannerMapper.update(banner);
    }

    @Override
    public Banner getById(Long id) {
       return bannerMapper.getById(id);
    }

    @Override
    public List<BannerVO> getBanner() {
        return bannerMapper.getBanner();
    }
}
